#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "MyinfoProfileDlg.h"
#include "../client/RegexpHandler.h"

#define GET_TEXT(id, var) \
	GetDlgItemText(id, buf, 1024); \
	var = buf;

#define ATTACH(id, var) var.Attach(GetDlgItem(id))

LRESULT MyinfoProfileDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if(currentProfileId != -1) {
		// FIXME disable this for now to stop potential dupes (ahh. fuck it, leave it enabled :p)
		//::EnableWindow(GetDlgItem(IDC_CLIENT_NAME), false);
		adding = false;
		getProfile();
	} else {
		adding = true;
		::EnableWindow(GetDlgItem(IDC_NEXT), false);
		::EnableWindow(GetDlgItem(IDC_BACK), false);
	}

	ATTACH(IDC_NAME, ctrlName);
	ATTACH(IDC_VERSION, ctrlVersion);
	ATTACH(IDC_TAG, ctrlTag);
	ATTACH(IDC_EXTENDED_TAG, ctrlExtendedTag);
	ATTACH(IDC_STATUS, ctrlStatus);
	ATTACH(IDC_NICK, ctrlNick);
	ATTACH(IDC_SHARED, ctrlShared);
	ATTACH(IDC_EMAIL, ctrlEmail);
	ATTACH(IDC_CHEATING_DESCRIPTION, ctrlCheatingDescription);
	ATTACH(IDC_CONNECTION, ctrlConnection);
	ATTACH(IDC_USE_EXTRA_VERSION, ctrlUseExtraVersion);
	ATTACH(IDC_ADD_LINE, ctrlAddLine);
	ATTACH(IDC_FORMATTED_TAG, ctrlFormattedTag);
	ATTACH(IDC_COMMENT, ctrlComment);
	ATTACH(IDC_PROFILE_RAW, ctrlRaw);

	for(int n=0;n<11;){
		ctrlRaw.AddString(RAWSETTING(n).c_str());
		++n;
	}

	ATTACH(IDC_REGEXP_TESTER_COMBO, ctrlRegExpCombo);

	ctrlRegExpCombo.AddString(CTSTRING(VERSION));
	ctrlRegExpCombo.AddString(CTSTRING(TAG));
	ctrlRegExpCombo.AddString(CTSTRING(DESCRIPTION));
	ctrlRegExpCombo.AddString(CTSTRING(NICK));
	ctrlRegExpCombo.AddString(CTSTRING(SHARED));
	ctrlRegExpCombo.AddString(CTSTRING(EMAIL));
	ctrlRegExpCombo.AddString(CTSTRING(STATUS));
	ctrlRegExpCombo.AddString(CTSTRING(CONNECTION));
	ctrlRegExpCombo.SetCurSel(0);

	SetDlgItemText(IDC_CLIPBOARD_ONE, SETTING(CLIPBOARD_NAME_ONE).c_str());
	SetDlgItemText(IDC_CLIPBOARD_TWO, SETTING(CLIPBOARD_NAME_TWO).c_str());
	SetDlgItemText(IDC_CLIPBOARD_THREE, SETTING(CLIPBOARD_NAME_THREE).c_str());
	SetDlgItemText(IDC_CLIPBOARD_FOUR, SETTING(CLIPBOARD_NAME_FOUR).c_str());
	SetDlgItemText(IDC_CLIPBOARD_FIVE, SETTING(CLIPBOARD_NAME_FIVE).c_str());
	SetDlgItemText(IDC_CLIPBOARD_SIX, SETTING(CLIPBOARD_NAME_SIX).c_str());

	params = ClientProfileManager::getInstance()->getParams();

	updateControls();
	
	CenterWindow(GetParent());
	return FALSE;
}

LRESULT MyinfoProfileDlg::onChange(WORD , WORD , HWND , BOOL& ) {
	updateAddLine();
	return 0;
}

LRESULT MyinfoProfileDlg::onChangeTag(WORD , WORD , HWND , BOOL& ) {
	updateAddLine();
	updateTag();
	return 0;
}

void MyinfoProfileDlg::updateTag() {
	char buf[BUF_LEN];
	string exp;
	GET_TEXT(IDC_TAG, exp); 
	formattedTag = RegexpHandler::formatRegExp(exp, params);
	ctrlFormattedTag.SetWindowText(formattedTag.c_str());
}

void MyinfoProfileDlg::updateAddLine() {
	addLine = Util::emptyString;
	TCHAR buf[BUF_LEN];

#define UPDATE \
	GetWindowText(buf, BUF_LEN-1); \
	addLine += buf; \
	addLine += ';';
	
	ctrlName.UPDATE;
	ctrlVersion.UPDATE;
	ctrlTag.UPDATE;
	ctrlExtendedTag.UPDATE;
	ctrlNick.UPDATE;
	ctrlShared.UPDATE;
	ctrlEmail.UPDATE;
	ctrlStatus.UPDATE;
	ctrlConnection.UPDATE;
	ctrlCheatingDescription.GetWindowText(buf, BUF_LEN-1);
	addLine += buf;

	ctrlAddLine.SetWindowText(addLine.c_str());
}

void MyinfoProfileDlg::getProfile() {
	ClientProfileManager::getInstance()->getMyinfoProfile(currentProfileId, currentProfile);

	name = currentProfile.getName();
	version = currentProfile.getVersion();
	tag = currentProfile.getTag();
	extendedTag = currentProfile.getExtendedTag();
	nick = currentProfile.getNick();
	shared = currentProfile.getShared();
	email = currentProfile.getEmail();
	status = currentProfile.getStatus();
	cheatingDescription = currentProfile.getCheatingDescription();
	rawToSend = currentProfile.getRawToSend();
	useExtraVersion = currentProfile.getUseExtraVersion();
	connection = currentProfile.getConnection();
	comment = currentProfile.getComment();
}

void MyinfoProfileDlg::updateVars() {
	TCHAR buf[1024];

	GET_TEXT(IDC_NAME, name);
	GET_TEXT(IDC_VERSION, version);
	GET_TEXT(IDC_TAG, tag);
	GET_TEXT(IDC_EXTENDED_TAG, extendedTag);
	GET_TEXT(IDC_NICK, nick);
	GET_TEXT(IDC_SHARED, shared);
	GET_TEXT(IDC_EMAIL, email);
	GET_TEXT(IDC_STATUS, status);
	GET_TEXT(IDC_CHEATING_DESCRIPTION, cheatingDescription);
	GET_TEXT(IDC_CONNECTION, connection);
	GET_TEXT(IDC_COMMENT, comment);
	useExtraVersion = (ctrlUseExtraVersion.GetCheck() == BST_CHECKED) ? 1 : 0;
	rawToSend = ctrlRaw.GetCurSel();
}

void MyinfoProfileDlg::updateControls() {
	ctrlName.SetWindowText(name.c_str());
	ctrlVersion.SetWindowText(version.c_str());
	ctrlTag.SetWindowText(tag.c_str());
	ctrlExtendedTag.SetWindowText(extendedTag.c_str());
	ctrlNick.SetWindowText(nick.c_str());
	ctrlShared.SetWindowText(shared.c_str());
	ctrlEmail.SetWindowText(email.c_str());
	ctrlStatus.SetWindowText(status.c_str());
	ctrlCheatingDescription.SetWindowText(cheatingDescription.c_str());
	ctrlAddLine.SetWindowText(addLine.c_str());
	ctrlConnection.SetWindowText(connection.c_str());
	ctrlComment.SetWindowText(comment.c_str());
	ctrlUseExtraVersion.SetCheck((useExtraVersion) ? BST_CHECKED : BST_UNCHECKED);
	ctrlRaw.SetCurSel(rawToSend);
	ctrlFormattedTag.SetWindowText(formattedTag.c_str());

	ctrlName.SetFocus();
}

LRESULT MyinfoProfileDlg::onNext(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {

	updateVars();

	currentProfile.setName(name);
	currentProfile.setVersion(version);
	currentProfile.setTag(tag);
	currentProfile.setExtendedTag(extendedTag);
	currentProfile.setNick(nick);
	currentProfile.setShared(shared);
	currentProfile.setEmail(email);
	currentProfile.setStatus(status);
	currentProfile.setCheatingDescription(cheatingDescription);
	currentProfile.setRawToSend(rawToSend);
	currentProfile.setUseExtraVersion(useExtraVersion);
	currentProfile.setConnection(connection);
	currentProfile.setComment(comment);
	ClientProfileManager::getInstance()->updateMyinfoProfile(currentProfile);

	//Sulan start svn77
	if(wID == IDC_NEXT && currentProfileId < itemCount-1)
		currentProfileId++;
	else if( wID == IDC_BACK && currentProfileId > 0)
		currentProfileId--;
	//End

	getProfile();
	updateControls();

	return 0;
}

//Sulan start 33
LRESULT MyinfoProfileDlg::onClip(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	switch(wID){
		case IDC_CLIPBOARD_ONE:		WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_ONE));		break;
		case IDC_CLIPBOARD_TWO:		WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_TWO));		break;
		case IDC_CLIPBOARD_THREE:	WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_THREE));	break;
		case IDC_CLIPBOARD_FOUR:	WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_FOUR));	break;
		case IDC_CLIPBOARD_FIVE:	WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_FIVE));	break;
		case IDC_CLIPBOARD_SIX:		WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_SIX));		break;
	}
	return 0;
}

LRESULT MyinfoProfileDlg::onMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	TCHAR buf[1024];
	string exp, text;
	GET_TEXT(IDC_REGEXP_TESTER_TEXT, text);
	switch(ctrlRegExpCombo.GetCurSel()) {
		case VERSION:	GET_TEXT(IDC_VERSION, exp);	break;
		case TAG: 
			{
				string version, versionExp, verTagExp, formattedExp;
				GET_TEXT(IDC_TAG, exp); 
				
				verTagExp = RegexpHandler::formatRegExp(exp, params);
				formattedExp = verTagExp;
				string::size_type j = formattedExp.find("%[version]");
				if(j != string::npos) {
					formattedExp.replace(j, 10, ".*");
					version = RegexpHandler::getVersion(verTagExp, text);
					GET_TEXT(IDC_VERSION, versionExp)
				}
				string temp = RegexpHandler::matchExp(formattedExp, text);
				if(temp.find(STRING(S_MISSMATCH)) != string::npos||temp.find(STRING(S_INVALID)) != string::npos){
					MessageBox(temp.c_str(), CTSTRING(REGEX_TESTER), MB_OK);
					return 0;
				}
				if(version.empty()) {
					MessageBox(CTSTRING(S_MATCH), CTSTRING(REGEX_TESTER), MB_OK);
					return 0;
				} else {
					MessageBox(RegexpHandler::matchExp(versionExp, version).c_str(), CSTRING(REGEX_TESTER), MB_OK);
					//Sulan start svn93
					// http://www.imperialnet.org/forum/viewtopic.php?p=9844#9844
					return 0;
					//End
				}
			}
		case DESCRIPTION: 
			{
				GET_TEXT(IDC_EXTENDED_TAG, exp);
				string::size_type j = exp.find("%[version2]");
				if(j != string::npos) {
					exp.replace(j, 11, ".*");
				}
				break;
			}
		case NICK: GET_TEXT(IDC_NICK, exp); break;
		case SHARED: GET_TEXT(IDC_SHARED, exp); break;
		case EMAIL: GET_TEXT(IDC_EMAIL, exp); break;
		case STATUS: GET_TEXT(IDC_STATUS, exp); break;
		case CONNECTION: GET_TEXT(IDC_CONNECTION, exp); break;
		default: dcdebug("We shouldn't be here!\n");
	}
	MessageBox(RegexpHandler::matchExp(exp, text).c_str(), CSTRING(REGEX_TESTER), MB_OK);
	return 0;
}

//Sulan start svn93
void MyinfoProfileDlg::phraseAddLine() {

	string param; 
	char buf[BUF_LEN];
	GET_TEXT(IDC_ADD_LINE, param);

	StringTokenizer<string> t(param, ';');
	StringList& sl = t.getTokens();
	if(sl.size() != 10){
		MessageBox(CTSTRING(INCORRECT_PARAMS), CSTRING(A_ERROR), MB_OK);
		return;
	}

	name = sl[0];
	version = sl[1];
	tag = sl[2];
	extendedTag = sl[3];
	nick = sl[4];
	shared = sl[5];
	email = sl[6];
	status = sl[7];
	connection = sl[8];
	cheatingDescription = sl[9];
	//BOOLS
	useExtraVersion = Util::toBool(sl[10]);
}
/**
 * @file
 * $Id: MyinfoProfileDlg.cpp 301 2005-08-18 12:13:39Z Pothead $
 */